package com.webex.ta.hydra.core.control;

import com.webex.ta.hydra.core.Command;
import com.webex.ta.hydra.core.ParamDescriptor;

/**
 * Created by IntelliJ IDEA.
 * User: vegaz
 * Date: 2010-8-26
 * Time: 15:44:33
 * To change this template use File | Settings | File Templates.
 */
public class LoopController extends GenericController {

    private transient int loopCount = 0;
    private static ParamDescriptor[] desc;
    static {
        desc = new ParamDescriptor[1];
        desc[0] = new ParamDescriptor("loop count");
    }

    @Override
    public ParamDescriptor[] getParamDescriptors(Command cmd) {
        return desc;
    }

    protected Command nextIsNull() throws NextIsNullException {
        reInitialize();
        if (endOfLoop()) {
//			if (!getContinueForever()) {
//				setDone(true);
//			} else {
            resetLoopCount();
//			}
            return null;
        }
        return next();
    }

    public void initialize() {
    }

    

    private boolean endOfLoop() {
        int totalLoops = params[0].getIntegerValue();
        return (totalLoops > -1) && loopCount >= totalLoops;
    }

    protected void incrementLoopCount() {
        loopCount++;
    }

    protected void resetLoopCount() {
        loopCount = 0;
    }

    protected void reInitialize() {
        setFirst(true);
        resetCurrent();
        incrementLoopCount();
//		recoverRunningVersion();
    }
}
